﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Sgml;
using System.Net;
using System.IO;

namespace BookStore.WebAnalyzer
{
    public class WebAnalyzerApi : IWebAnalyzerApi
    {
        public SgmlReader sgmlReader = null;
        public WebAnalyzerApi(Sgml.SgmlReader sgmlReader)
        {
            this.sgmlReader = sgmlReader;
            this.sgmlReader.DocType = "HTML";
            this.sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
            this.sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;

        }
        #region IWebAnalyzer Members

        public XmlDocument getXmlDocumentFromUrl(string url)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.Timeout = int.MaxValue;
            req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
            HttpWebResponse resp = req.GetResponse() as HttpWebResponse;

            using (Stream input = resp.GetResponseStream())
            {
                this.sgmlReader.InputStream = new StreamReader(input,Encoding.GetEncoding("GBK"));
                XmlDocument doc = new XmlDocument();
                doc.PreserveWhitespace = true;
                doc.XmlResolver = null;
                doc.Load(sgmlReader);
                return doc;
            }
            
        }

        #endregion
    }

    public interface IWebAnalyzerApi
    {
        XmlDocument getXmlDocumentFromUrl(string url);
    }
}
